<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Finance extends Admin_Controller {

	function __construct ()
	{
		parent::__construct();		
            $this->load->model('money_m');
            $this->load->model('user_m');
            $this->load->model('good_m');
            $this->load->model('shop_m');
            $this->load->model('order_m');
            $this->load->model('credit_m');
            $this->load->model('message_m');
       		$this->load->library('pagination');
            
			$this->load->library('myclass');
	}
	//充值管理
	public function recharge($page=1)
	{
		$limit = 20;
        $config['uri_segment'] = 4;
        $config['use_page_numbers'] = TRUE;
        $config['base_url'] = site_url('manage/finance/recharge/');
        $config['total_rows'] = $this->money_m->count_chongzhi(0);
        $config['per_page'] = $limit;
        $config['num_links'] = 10;
        $this->pagination->initialize($config);
        $start = ($page-1)*$limit;
        $data['pagination'] = $this->pagination->create_links();
		$recharge = $this->money_m->get_recharge_list($start,$limit,0,'desc');
		if($recharge){
			foreach ($recharge as $k => $val) {
				$userinfo = $this->user_m->get_user_by_user_id($val['user_id']);
				$recharge[$k]['user_name'] = $userinfo['user_name'];
			}
		}
		$data['users'] = $this->user_m->get_all_users();

		$data['recharge'] = $recharge;
		$this->load->view('finance/recharge',$data);
	} 
	//删除用户的充值记录
	public function delrecharge($id){
		$result = $this->money_m->del_moneys_by_id($id);
		if($result){
			show_message("删除用户的充值记录成功",site_url(X_URL.'finance/recharge'),1);	
		}
		
	}

	//提现管理
	public function cash($page=1){
		$limit = 20;
        $config['uri_segment'] = 4;
        $config['use_page_numbers'] = TRUE;
        $config['base_url'] = site_url('manage/finance/cash/');
        $config['total_rows'] = $this->db->count_all('cashs');
        $config['per_page'] = $limit;
        $config['num_links'] = 10;
        $this->pagination->initialize($config);
        $start = ($page-1)*$limit;
        $data['pagination'] = $this->pagination->create_links();
		$cash = $this->money_m->get_cash_list($start,$limit,0,$order="desc");
		if($cash){
			foreach ($cash as $k => $val) {
				$userinfo = $this->user_m->get_user_by_user_id($val['user_id']);
				$cash[$k]['user_name'] = $userinfo['user_name'];
				
			}
		}
		$data['users'] = $this->user_m->get_all_users();

		$data['cash'] = $cash;
		$this->load->view('finance/cash',$data);
	}
	//个人财务
	public function personal($page = 1){
		$limit = 20;
        $config['uri_segment'] = 4;
        $config['use_page_numbers'] = TRUE;
        $config['base_url'] = site_url('manage/finance/personal/');
        $config['total_rows'] = $this->db->count_all('moneys');
        $config['per_page'] = $limit;
        $config['num_links'] = 10;
        $this->pagination->initialize($config);
        $start = ($page-1)*$limit;
        $data['pagination'] = $this->pagination->create_links();
		$personal = $this->money_m->get_all_recharge_list($start,$limit);
		if($personal){
			foreach ($personal as $k => $val) {
				$userinfo = $this->user_m->get_user_by_user_id($val['user_id']);
				$personal[$k]['user_name'] = $userinfo['user_name'];
			}
		}
		//查询所有的用户
		$data['users'] = $this->user_m->get_all_users();
		$data['personal'] = $personal;
		$this->load->view('finance/personal',$data);
	}
	//删除个人财务
	public function delpersonal($id){
		$result = $this->money_m->del_moneys_by_id($id);
		if($result){
			show_message("删除个人财务记录成功",site_url(X_URL.'finance/personal'),1);	
		}
	}
	//公司财务明细
	public function shop($page=1){
		$limit = 20;
        $config['uri_segment'] = 4;
        $config['use_page_numbers'] = TRUE;
        $config['base_url'] = site_url('manage/finance/shop/');
        $config['total_rows'] = $this->money_m->count_all_sucess_order(0);
        $config['per_page'] = $limit;
        $config['num_links'] = 10;
        $this->pagination->initialize($config);
        $start = ($page-1)*$limit;
        $data['pagination'] = $this->pagination->create_links();
		$orders = $this->money_m->get_orders_suceses($start,$limit);
		if($orders){
			foreach ($orders as $k => $val) {
				$goodinfo = $this->good_m->get_shop_goods_by_id($val['good_id']);
                $shopinfo = $this->shop_m->get_shop_user_by_id($val['shop_id']);
				$userinfo = $this->user_m->get_user_by_user_id($val['user_id']);
				$orders[$k]['user_name'] = $userinfo['user_name'];
				$orders[$k]['shop_title'] = $shopinfo['shop_title'];
				$orders[$k]['good_title'] = $goodinfo['good_title'];

			}
		}
		$data['shops']=$this->shop_m->get_all_shops();
		$data['orders'] = $orders;
		$this->load->view("finance/shop",$data);
	}
	//删除商家财务
	public function delorders($id){
		$result = $this->order_m->del_order_by_id($id);
		if($result){
			show_message("删除商家财务记录成功",site_url(X_URL.'finance/shop'),1);	
		}
	}
	//删除
	public function delcash($id){
		$this->money_m->del_moneys_by_id($id);
        show_message('删除提现记录',site_url(X_URL.'finance/cash'),1); 

	}

	//处理积分
	public function credit_lock($id){
		$data['credit_lock'] = 2;
		$this->credit_m->save_credit_lock($id,$data);
		show_message('处理完成',site_url(X_URL.'finance/cash'));
	}
	//核实账号
	public function type($id=0){
		//获取申请提现的信息
		$info = $this->money_m->get_cash_by_id($id);
		$cash_type=$info['cash_type'];
		if($cash_type == 0){
			$data['cash_type'] = 1;
			$data['cash_update_time']=time();
			$result=$this->money_m->save_cash_type($id,$data);
			if($result){
				show_message('已确认用户账号信息，请尽快打款',site_url(X_URL.'finance/cash'),1);
			}
		}else if($cash_type == 1){

			$data['cash_type'] = 2;
			$data['cash_update_time']=time();
			$result=$this->money_m->save_cash_type($id,$data);
			if($result){
				$user_id=$info['user_id'];
				$moneys= $info['cash_num'];
				//获取该用户的余额
				$money=$this->money_m->get_money_by_user_id($user_id);
				$user_money=$money['user_money'];
				$new_money = $user_money-$moneys;
				$arr['user_money'] = $new_money;
				$results = $this->money_m->update_user_moneys_by_id($user_id,$arr);
				if($results){
					//将数据插入到money表中
					$arr=array(
						'user_id'=>$user_id,
						'money_num'=>$moneys,
						'money_type'=>4,
						'money_lock'=>2,
						'money_time'=>time(),
					);
					if($this->money_m->add_moneys($arr)){
						//提现信息存到message表中
                    $jlmsg['message_txt'] = '提现成功，商家已经线下给你打款<em>'.$moneys.'元</em>。<a href="'.site_url("member").'">查看信息</a>';
                    $jlmsg['message_type'] =8;
                    $jlmsg['message_to_uid'] =$user_id;
                    $jlmsg['message_time'] =time();
                    $jlmsg['message_lock'] =1;
                    $this->message_m->add_massage($jlmsg);
						show_message('已经线下打款成功',site_url(X_URL.'finance/cash'),1);
					}
					
				}
				
			}

		}
	}
	//判断是否是无效单
	public function types($id=0){
		//获取申请提现的信息
		$info = $this->money_m->get_cash_by_id($id);
		$user_id=$info['user_id'];
		$moneys=$info['cash_num'];
		$cash_type=$info['cash_type'];
		if($cash_type == 0){
			$data['cash_type'] = 3;
			$data['cash_update_time']=time();
			$result=$this->money_m->save_cash_type($id,$data);
			if($result){
			//提现信息存到message表中
	            $jlmsg['message_txt'] = '提现申请无效，商家已经拒绝<em>'.$moneys.'元</em>。请核实您的申请信息<a href="'.site_url("member").'">查看信息</a>';
	            $jlmsg['message_type'] =9;
	            $jlmsg['message_to_uid'] =$user_id;
	            $jlmsg['message_time'] =time();
	            $jlmsg['message_lock'] =1;
                $this->message_m->add_massage($jlmsg);
				show_message('已确认此单为无效单',site_url(X_URL.'finance/cash'),2);
			}
		}
	}

	//删除已经处理的提现记录
	public function del($id){
		$results = $this->money_m->del_cash_by_id($id);
		if($results){
			show_message('删除提现记录成功',site_url(X_URL.'finance/cash'),1);
		}
	}

	//查询单个用户
	public function search($user_id=0,$order='desc',$page=1){
        $get = $this->input->get();
        $user_id = !empty($get['user_id']) ? $get['user_id'] : $user_id;
        $order =!empty($get['order']) ? $get['order'] : $order;
        $limit = 20;
        $config['uri_segment'] = 6;
        $config['use_page_numbers'] = TRUE;
        $config['base_url'] = site_url('manage/finance/search/'.$user_id.'/'.$order);
        $config['total_rows'] = $this->money_m->count_all_keyword_money($user_id);
        $config['per_page'] = $limit;
        $config['num_links'] = 10;
        $this->pagination->initialize($config);
        $start = ($page-1)*$limit;
        $data['pagination'] = $this->pagination->create_links();
		$personal = $this->money_m->get_all_recharge_keyword_list($start,$limit,$user_id,$order);
		if($personal){
			foreach ($personal as $k => $val) {
				$userinfo = $this->user_m->get_user_by_user_id($val['user_id']);
				$personal[$k]['user_name'] = $userinfo['user_name'];
			}
		}
		//查询所有的前台用户
		$data['users'] = $this->user_m->get_all_users();
		$data['personal'] = $personal;
		$this->load->view('finance/personal',$data);

    }
    //查询商家
    public function search_shop($shop=0,$order='desc',$page=1){
    	$get = $this->input->get();
        $shop_id = !empty($get['shop_id']) ? $get['shop_id'] : $shop;
        $order =!empty($get['order']) ? $get['order'] : $order;
        $limit = 20;
        $config['uri_segment'] = 6;
        $config['use_page_numbers'] = TRUE;
        $config['base_url'] = site_url('manage/finance/search_shop/'.$shop_id.'/'.$order);
        $config['total_rows'] = $this->money_m->count_all_sucess_order($shop_id);
        $config['per_page'] = $limit;
        $config['num_links'] = 10;
        $this->pagination->initialize($config);
        $start = ($page-1)*$limit;
        $data['pagination'] = $this->pagination->create_links();
		$orders = $this->money_m->get_orders_suceses_order_by($start,$limit,$shop_id,$order);
		if($orders){
			foreach ($orders as $k => $val) {
				$goodinfo = $this->good_m->get_shop_goods_by_id($val['good_id']);
                $shopinfo = $this->shop_m->get_shop_user_by_id($val['shop_id']);
				$userinfo = $this->user_m->get_user_by_user_id($val['user_id']);
				$orders[$k]['user_name'] = $userinfo['user_name'];
				$orders[$k]['shop_title'] = $shopinfo['shop_title'];
				$orders[$k]['good_title'] = $goodinfo['good_title'];

			}
		}
		$data['shops']=$this->shop_m->get_all_shops();
		$data['orders'] = $orders;
		$this->load->view("finance/shop",$data);
    }
    public function recharge_search($user_id=0,$order='desc',$page=1){
    	$get = $this->input->get();
        $user_id = !empty($get['user_id']) ? $get['user_id'] : $user_id;
        $order =!empty($get['order']) ? $get['order'] : $order;
        $limit = 30;
        $config['uri_segment'] = 6;
        $config['use_page_numbers'] = TRUE;
        $config['base_url'] = site_url('manage/finance/recharge_search/'.$user_id.'/'.$order);
        $config['total_rows'] = $this->money_m->count_chongzhi($user_id);
        $config['per_page'] = $limit;
        $config['num_links'] = 10;
        $this->pagination->initialize($config);
        $start = ($page-1)*$limit;
        $data['pagination'] = $this->pagination->create_links();
        $recharge = $this->money_m->get_recharge_list($start,$limit,$user_id,$order);
		if($recharge){
			foreach ($recharge as $k => $val) {
				$userinfo = $this->user_m->get_user_by_user_id($val['user_id']);
				$recharge[$k]['user_name'] = $userinfo['user_name'];
			}
		}
		$data['users'] = $this->user_m->get_all_users();

		$data['recharge'] = $recharge;
		$this->load->view('finance/recharge',$data);
    }

    public function cash_search($user_id=0,$order='desc',$page=1){
    	$get = $this->input->get();
        $user_id = !empty($get['user_id']) ? $get['user_id'] : $user_id;
        $order =!empty($get['order']) ? $get['order'] : $order;
        $limit = 30;
        $config['uri_segment'] = 6;
        $config['use_page_numbers'] = TRUE;
        $config['base_url'] = site_url('manage/finance/cash_search/'.$user_id.'/'.$order);
        $config['total_rows'] = $this->money_m->count_cash($user_id);
        $config['per_page'] = $limit;
        $config['num_links'] = 10;
        $this->pagination->initialize($config);
        $start = ($page-1)*$limit;
        $data['pagination'] = $this->pagination->create_links();
        $cash = $this->money_m->get_cash_list($start,$limit,$user_id,$order);
		if($cash){
			foreach ($cash as $k => $val) {
				$userinfo = $this->user_m->get_user_by_user_id($val['user_id']);
				$cash[$k]['user_name'] = $userinfo['user_name'];
				
			}
		}
		$data['users'] = $this->user_m->get_all_users();

		$data['cash'] = $cash;
		$this->load->view('finance/cash',$data);
    }
}

